<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span
              >江苏传智播客教育科技股份有限公司</span
            >
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" :default-expand-all="true">
          <template v-slot="scope">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ scope.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ scope.data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(scope.data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(scope.data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hDel(scope.data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog  :title="isEdit? '编辑部门' : '新增部门'" :visible.sync="dialogVisible" >
      <deptDialog :pid="this.curid" @success="hSuccess" :isEdit="isEdit" v-if="dialogVisible" @close="hclose" :originList="originList"></deptDialog>
    </el-dialog>
  </div>
</template>

<script>
// 导入api
import { getDepartments,delDepartments } from "@/api/departments.js";
// 导入 数组转树形的方法
import { array2tree } from "@/utils/index";
// 导入子组件
import  deptDialog  from "./deptDialog.vue";
export default {
  // 注册组件
  components: { deptDialog },
  data() {
    return {
      // 部门列表数据
      list: [],
      // 弹层盒子显示隐藏
      dialogVisible:false,
      // 父传子
      curid : '',
      isEdit : false,
      // 传递给子组件 来做表单验证
      originList:[]
    };
  },
  created() {
    // 查询企业的部门列表
    this.setDepartments();
  },
  methods: {
    async setDepartments() {
      // 查询企业的部门列表
      const res = await getDepartments();
      // console.log(res);
      // console.log(this.data)
      // 调用数组转树形
      // 组装一份列表数据给子组件 做表单校验
      this.originList = res.data.depts.map(item=> {
        return{
          id: item.id,
          pid: item.pid,
          name: item.name,
          code: item.code
        }
      })
      this.list = array2tree(res.data.depts, "");
    },
    // 添加子部门
    hAdd(id){
      // 打开弹层盒子
      this.dialogVisible = true
      // 作用域插槽传递id
      this.curid = id
      this.isEdit = false
    },
    // 子传父 的事件
    hSuccess(){
      // 关闭弹层
      this.dialogVisible = false,
      // 更新数据
      this.setDepartments();
    },
    // 删除数据
    hDel(id){
       this.$confirm('此操作将删除部门, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          // 调用删除api 
          await delDepartments(id)
          // 刷新页面
          this.setDepartments();
        }).catch(() => {
          // 用户取消退出   
        });
    },
    hEdit(id){
      this.dialogVisible = true
      this.curid = id
      this.isEdit = true
    },
    hclose(){
      this.dialogVisible = false
    }
  },
};
</script>

